package com.zp.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class SerializableTest1{
	public static void main(String[] args) throws Exception {
		Student p1 = new Student(20, "zhangsan", 4.55);
		Student p2 = new Student(50, "lisi", 4.67);
		Student p3 = new Student(10, "wangwu", 17.78);
		
		/**
		 * 序列化：Output-write
		 */
		OutputStream os = new FileOutputStream("D:/lisi/obj.txt");
		ObjectOutputStream oos = new ObjectOutputStream(os);
		
		oos.writeObject(p1);
		oos.writeObject(p2);
		oos.writeObject(p3);
		
		oos.close();
		
		/**
		 * 反序列化：Input-read
		 */
		InputStream is = new FileInputStream("D:/lisi/obj.txt");
		ObjectInputStream ois = new ObjectInputStream(is);
		
		Student s;
		for(int i=0;i<3;i++){
			s = (Student)ois.readObject();
			System.out.println(s.getId() + "," + s.getName() + "," + s.getHeight());
		}
		ois.close();
	}
}

